package com.dx.common.base.core;

import java.util.Map;

import com.dx.common.base.bean.CustomerBean;
import com.dx.common.base.util.ThreadPoolManager;
//import com.huawei.eshop.common.util.Constants;
import com.jfinal.plugin.activerecord.Record;

/**
 * 
 * 操作日志管理
 * 
 * @author wKF52599
 * @version [NGESHOP V1.0, Nov 19, 2011]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class LogManager
{
    private static ThreadPoolManager threadPool = ThreadPoolManager.getInstance("logManager"); 
    
    private static ThreadLocal local = new ThreadLocal();
    
    private final static String OPER_SUCCESS="1";
    
    private final static String OPER_FAIL="1";
    
    
    /**
     * 初始化
     */
    static
    {
        initialize();
    }
    
    /**
     * 初始化
     * 
     * @see [类、类#方法、类#成员]
     */
    public static void initialize()
    {
        Record log = new Record();
        local.set(log);
        
        setOperstatus(OPER_SUCCESS);//success
        
    }
    
    /**
     * 返回logbean对象
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static Record getLog()
    {
        Record log = (Record)local.get();
        if (log == null)
        {
            log = new Record();
            setLog(log);
            setOperstatus(OPER_SUCCESS);
        }
        return log;
    }
    
    /**
     * 存入日志对象
     * 
     * @param log
     * @see [类、类#方法、类#成员]
     */
    private static void setLog(Record log)
    {
        local.set(log);
    }
    
    /**
     * 将session中的用户信息记入操作日志s
     * 
     * @param bean
     * @see [类、类#方法、类#成员]
     */
    public static void setCustomerBean(CustomerBean bean)
    {
        Record log = getLog();
        //log.setCust_id(bean.getCustId());
        log.set("account",bean.getAccount());
        log.set("name",bean.getName());
        log.set("email",bean.getEmail());
        //log.set("tel",bean.getTel());
        //log.set("status",bean.getStatus());
        setLog(log);
    }
    
    /**
     * 未登录用户的操作日志记录
     * 
     * @param ipaddr
     * @param macaddr
     * @see [类、类#方法、类#成员]
     */
    public static void setVisitor(String ipaddr, String macaddr)
    {
        Record log = getLog();
        // id记录
        log.set("account","未登陆客户");
        log.set("name","未登陆客户");
        log.set("email","");
        //log.set("tel","");
       //log.set("status","");
        setLog(log);
    }
    /**
     * <一句话功能简述>设置代理ip
     * <功能详细描述>
     * @param proxyIp
     * @see [类、类#方法、类#成员]
     */   
    public static void setProxyIp(String proxyIp)
    {
        Record log = getLog();
        log.set("proxy_ip",proxyIp);
        setLog(log);
    }

    /**
     * <一句话功能简述>设置用户浏览器信息
     * <功能详细描述>
     * @param userAgent
     * @see [类、类#方法、类#成员]
     */
    public static void setUserAgent(String userAgent)
    {
        Record log = getLog();
        log.set("user_agent",userAgent);
        setLog(log);
    }   
    /**
     * 设置业务类型
     * 
     * @param value
     * @see [类、类#方法、类#成员]
     */
    public static void setBiz_id(String biz_id)
    {
        Record log = getLog();
        log.set("biz_id",biz_id);
        setLog(log);
    }

    /**
     * 设置操作记录信息
     * 
     * @param describe
     * @see [类、类#方法、类#成员]
     */
    public static void setDescribe(String describe)
    {
        Record log = getLog();
        log.set("describe",describe);
        setLog(log);
    }
    
    /**
     * 增加操作记录信息
     * 
     * @param describe
     * @see [类、类#方法、类#成员]
     */
    public static void appendDescribe(String describe)
    {
        Record log = getLog();
        if (log.get("describe") != null && log.get("describe").toString().length() > 0)
        {
        	log.set("describe",log.get("describe") + "~" + describe);
        }
        else
        {
        	log.set("describe",describe);
        }
        setLog(log);
    }
    
    /**
     * 增加异常信息
     * 
     * @param describe
     * @see [类、类#方法、类#成员]
     */
    public static void appendExceptionInfo(Exception e)
    {
        Record log = getLog();
        // 保存错误堆栈信息
 		String message = e.getMessage() + "<br/>";
 		StackTraceElement[] trace = e.getStackTrace();
 		for (int i = 0; i < trace.length; i++) {
 			message += "\tat " + trace[i].toString() + "<br/>";
 		}
 		
 		message=(log.get("exception_info")==null?"":log.get("exception_info"))+message;
 		
 		if (message.length() > 3800) {
 			message = message.substring(0, 3800);
 		}
        log.set("exception_info",message);
        
        setLog(log);
    }
        
    /**
     * 设置操作结果状态
     * 
     * @param value
     * @see [类、类#方法、类#成员]
     */
    public static void setOperstatus(String operaStatus)
    {
        Record log = getLog();
        log.set("operate_status",operaStatus);
        setLog(log);
    }
    
    /**
     * 设置seesionid
     * 
     * @param value
     * @see [类、类#方法、类#成员]
     */
    public static void setSessionid(String sessionId)
    {
        Record log = getLog();
        log.set("session_id",sessionId);
        setLog(log);
    }
    
    /**
     * 设置channel_type
     */
    public static void setChannel_type(String channelType)
    {
        Record log = getLog();
        log.set("channel_type",channelType);
        setLog(log);
    }
    
    /**
     * 设置biz_begindate
     */
    public static void setBiz_begindate(String value)
    {
        Record log = getLog();
        log.set("biz_begin_date",value);
        setLog(log);
    }
    
    /**
     * 设置biz_enddate
     */
    public static void setBiz_endate(String value)
    {
        Record log = getLog();
        log.set("biz_end_date",value);
        setLog(log);
    }
    
    /**
     * 设置products_id
     */
    /*public static void setProducts_id(String value)
    {
        Record log = getLog();
        log.setProducts_id(value);
        setLog(log);
    }*/
    
    /**
     * 记录产品id <可多次添加，各产品ID之间以“,”分割>
     * 
     * @param productsId 产品ID
     * @see [类、类#方法、类#成员]
     */
    /*public static void appendProducts_id(String productsId)
    {
        Record log = getLog();
        if (log.getProducts_id() != null && log.getProducts_id().length() > 0)
        {
            log.setProducts_id(log.getProducts_id() + "," + productsId);
        }
        else
        {
            log.setProducts_id(productsId);
        }
        setLog(log);
    }*/
    
    /**
     * 设置biz_before_change
     */
    public static void setBiz_before_change(String value)
    {
        Record log = getLog();
        log.set("biz_before_change",value);
        setLog(log);
    }
    
    /**
     * 记录修改之前的业务值 <可多次添加，以“,”分割>
     */
    public static void appendBiz_before_change(String value)
    {
        Record log = getLog();
        if (log.get("biz_before_change") != null && log.getStr("biz_before_change").length() > 0)
        {
        	log.set("biz_before_change", log.getStr("biz_before_change") + "," + value);
        }
        else
        {
        	log.set("biz_before_change",value);
        }
        setLog(log);
    }
    
    /**
     * 设置biz_after_change
     */
    public static void setBiz_after_change(String value)
    {
        Record log = getLog();
        log.set("biz_after_change",value);
        setLog(log);
    }

    /**
     * 记录修改之后的业务值 <可多次添加，以“,”分割>
     */
    public static void appendBiz_after_change(String value)
    {
        Record log = getLog();
        if (log.get("biz_after_change") != null && log.getStr("biz_after_change").length() > 0)
        {
        	log.set("biz_after_change", log.getStr("biz_after_change") + "," + value);
        }
        else
        {
        	log.set("biz_after_change",value);
        }
        setLog(log);
    }
    
    
    /**
     * 设置invoke_method
     * 值为action方法中调用的该action中的其他方法
     */
    public static void setInvoke_method(String value)
    {
        Record log = getLog();
        log.set("invoke_method",value);
        setLog(log);
    }
    
    
    /**
     * 设置action_url
     */
    public static void setAction_url(String value)
    {
        Record log = getLog();
        log.set("action_url",value);
        setLog(log);
    }
    
    public static void setAction_params(Map<String,String[]> value)
    {
        Record log = getLog();
        log.set("action_params","action_params");
        setLog(log);
    }

    /**
     * 设置interface_param
     */
    public static void setInterface_param(String value)
    {
        Record log = getLog();
        log.set("action_params",value);
        setLog(log);
    }
    
    /**
     * <一句话功能简述>设置array转发ip
     * <功能详细描述>
     * @param forwardIp
     * @see [类、类#方法、类#成员]
     */ 
    public static void setForwardIp(String forwardIp)
    {
        Record log = getLog();
        log.set("forward_ip",forwardIp);
        setLog(log);
    }
    
    /**
     * 写日志
     */
    /*public static void write(boolean write)
    {
        Record log = getLog();
        log.setWrite(write);
        setLog(log);
    }*/
    
    /**
     * 写入日志表
     * 
     * @see [类、类#方法、类#成员]
     */
    public static void logging()
    {
    	
        Record log = (Record)local.get();
        //设置业务类型的业务可以写入操作日志
        if ( log.get("biz_id") != null && !"".equals(log.getStr("biz_id")))
        {
        	System.out.println("log:"+log);
        	//开始执行写入数据库
        	threadPool.addTask(new LogWriter(log));
        	System.out.println("写入日志表...");
        }
    }
    
    /**
     * 实例销毁
     * 
     * @see [类、类#方法、类#成员]
     */
    public static void destory()
    {
        local.remove();
    }
    
}
